package com.woniu.controller;

import com.woniu.service.MarkPaperService;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.entity.xql.MarkPapers;
import com.woniuxy.commons.enums.TestBankEnum;
import com.woniuxy.commons.service.RemoteAnswerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @program: bzeducation
 * @author: Xingqilin
 * @create: 2020-07-27 14:51
 * @description:阅卷模块
 */
@RestController
@RequestMapping("/mark")
public class MarkPaperController {
    @Resource
    MarkPaperService markPaperService;

    @RequestMapping(value = "/getPapers/{paperSubject}/{paperType}/{answerClass}/{answerDeal}", method = RequestMethod.GET)
    public List<MarkPapers> getPapersName(@PathVariable long paperSubject, @PathVariable long paperType, @PathVariable String answerClass, @PathVariable long answerDeal) {
        List<MarkPapers> papersName = markPaperService.getPapersName(paperSubject, paperType, answerClass, answerDeal);
        return papersName;
    }

    @RequestMapping(value = "/getSheetList/{paperID}/{status}", method = RequestMethod.GET)
    public List<AnswerSheet> getAnswerSheet(@PathVariable Integer paperID, @PathVariable Integer status) {
        List<AnswerSheet> answerSheet = markPaperService.getAnswerSheet(paperID, status);
        return answerSheet;
    }

    /**
     * @param paperId :试卷ID
     * @param userId  : 用户ID
     * @description : 自动阅卷方法，遍历选择判断题判分
     */
    @RequestMapping(value = "/startMark/{paperId}/{userId}", method = RequestMethod.GET)
    public Map<Integer, Object> startMark(@PathVariable Integer paperId, @PathVariable Integer userId) {
//        返回答题卡答案列表
        Map<Integer, Object> backSheet = new HashMap<>();
        ReplySheet computer = new ReplySheet();
        AnswerSheet detailAnswerSheet = markPaperService.getDetailAnswerSheet(paperId, userId, computer);
        backSheet.put(0, computer);
        //        获取试卷问题
        List<TestQuestion> paperQuestions = detailAnswerSheet.getTestPaper().getPaperQuestions();
//        获取学生答案
        List<ReplySheet> sheetAnswers = detailAnswerSheet.getSheetAnswers();

        int answerOffset = 0;
        for (int i = 0; i < paperQuestions.size(); i++) {
            TestQuestion testQuestion = paperQuestions.get(i);
//            判断问题类型
            if (testQuestion.getPaperQuestionBank() == TestBankEnum.VACANCY_BANK.getIndex()) {
                System.out.println("VACANCY_BANK:" + i);
//                转换成对应对象
                ReplySheet replySheet = sheetAnswers.get(i);
                backSheet.put(i + 1, replySheet);
            } else if (testQuestion.getPaperQuestionBank() == TestBankEnum.MATERIAL_BANK.getIndex()) {
                System.out.println("MATERIAL_BANK:" + i);
                MaterialBank materialBank = (MaterialBank) testQuestion.getPaperDetailQuestion();
                answerOffset += materialBank.getSelectBanks().size();
                List<VacancyBank> vacancyBank = materialBank.getVacancyBanks();
                for (int vacancyIndex = 0; vacancyIndex < vacancyBank.size(); vacancyIndex++) {
                    backSheet.put(i + 1 + vacancyIndex, sheetAnswers.get(i + vacancyIndex + answerOffset));
                    answerOffset++;
                    System.out.println(answerOffset);
                }
                answerOffset += materialBank.getJudgeBanks().size() - 1;
            } else if (testQuestion.getPaperQuestionBank() == TestBankEnum.CLOZE_BANK.getIndex()) {
                ClozeBank clozeBank = (ClozeBank) testQuestion.getPaperDetailQuestion();
                answerOffset += clozeBank.getClozeSelects().size() - 1;
            } else if (testQuestion.getPaperQuestionBank() == TestBankEnum.REVISE_BANK.getIndex()) {
                System.out.println("REVISE_BANK:" + answerOffset);
                ReplySheet replySheet = sheetAnswers.get(i + answerOffset);
                backSheet.put(i + 1, replySheet);
            } else if (testQuestion.getPaperQuestionBank() == TestBankEnum.WRITER_BANK.getIndex()) {
                System.out.println("WRITER_BANK:" + answerOffset);
                ReplySheet replySheet = sheetAnswers.get(i + answerOffset);
                backSheet.put(i + 1, replySheet);
            }

        }

        return backSheet;
    }
/**
 * @param replySheet :答题卡对应对象
 * @description：人工阅卷返回问题封装对象，写入分数
 * */
@RequestMapping(value = "/markOne", method = RequestMethod.PUT)
    public String oneTopicPoint(@RequestBody ReplySheet replySheet) {
        System.out.println(replySheet);
        String s = markPaperService.oneTopicPoint(replySheet);
        return s;
    }
}